package com.jfinal.dingtalk.api.base;

import com.jfinal.dingtalk.api.ApiResult;
import com.jfinal.dingtalk.utils.JsonUtils;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;

import java.util.HashMap;
import java.util.Map;

/**
 * 抽象公共api基础
 * Created by L.cm on 2016/6/2.
 */
public abstract class BaseApi {

    private static String getApiUrl(String propKey) {
        Prop result = PropKit.use("dingtalk_api.properties");
        String url = result.get(propKey);
        String accessToken = "";
        return url.replace("{access_token}", accessToken);
    }

    protected static ApiResult post(String propKey, Map<String, Object> data) {
        String url = getApiUrl(propKey);
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json");
        String jsonResult = HttpKit.post(url, JsonUtils.toJson(data), headers);
        return new ApiResult(jsonResult);
    }
}
